//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __CHANNEL_PACKAGE_H__
#define __CHANNEL_PACKAGE_H__

#include "ChannelIO.h"

class ChannelPackage
{
public:
    static const unsigned int MAX_DATA_SIZE = 0x800;

private:
    static unsigned int g_SentPackageCount;
    static unsigned int g_ReceivedPackageCount;

protected:
    unsigned int m_channelNumber;
    unsigned int m_packageNumber; // global increasing number

public:
    static void WriteInteger(char* buffer, int number);
    static int ReadInteger(ReadInputStream is, int* number);
    static int ReadInteger(char* buffer);
    static int CRCCheck(void* data, unsigned int size);
    static unsigned int NextPackageNumber();
    static inline int RoundSize(int size)
    {
        return ((size&0x3) ? ((size&~0x3)+0x4) : size);
    }

    virtual unsigned int GetPackageSize() = 0;
    virtual void WriteBuffer(char* buffer) = 0;
    virtual int Write(WriteOutputStream os);
    virtual int Read(ReadInputStream is) = 0;

    inline unsigned int GetChannelNumber() const
    {
        return m_channelNumber;
    }
    inline void SetChannelNumber(unsigned int channelNumber)
    {
        m_channelNumber = channelNumber;
    }
    inline unsigned int GetPackageNumber() const
    {
        return m_packageNumber;
    }
    inline void SetPackageNumber(unsigned int packageNumber)
    {
        m_packageNumber = packageNumber;
    }
};

#endif //__CHANNEL_PACKAGE_H__
